using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;


namespace MMYY.BLL
{
    public class Data
    {
        private string sActionUrl = "http://www.cmsplugin.net/tool/tool.asp?action=http2qvod";
        private IDAL.IData dal = DALFactory.DataAccess.CreateData();

        public IList<Model.Data> GetByPage(int typeid,string keywords, int desc, int pageindex, bool isFromPic)
        {
            return dal.GetByPage(typeid,keywords, desc, pageindex, isFromPic);
        }

        public string GetPagesBar(int typeid,string keywords, int desc,int pageindex, bool isFromPic)
        {
            int pageCount = dal.GetByPageCount(typeid,keywords, desc, isFromPic);
            string pages = string.Empty;
            string first = "<a href=\"javascript:void(0)\" onClick=\"go_page('movie','pic/index.aspx?typeid=" + typeid + "&desc=" + desc + "&page=1')\"><<</a>";
            string prve = "<a href=\"javascript:void(0)\" onClick=\"go_page('movie','pic/index.aspx?typeid=" + typeid + "&desc=" + desc + "&page=" + (pageindex - 1) + "')\"><</a>";
            string next = "<a href=\"javascript:void(0)\" onClick=\"go_page('movie','pic/index.aspx?typeid=" + typeid + "&desc=" + desc + "&page=" + (pageindex + 1) + "')\">></a>";
            string last = "<a href=\"javascript:void(0)\" onClick=\"go_page('movie','pic/index.aspx?typeid=" + typeid + "&desc=" + desc + "&page=" + pageCount + "')\">>></a>";
            string one = "<a href=\"javascript:void(0)\" onClick=\"go_page('movie','pic/index.aspx?typeid=" + typeid + "&desc=" + desc + "&page=";
            
            
            for (int i = pageindex - 2; i <= pageindex + 2; ++i)
            {
                if (i < 1 || i > pageCount) continue;
                pages += one + i + "')\""; if (pageindex == i) pages += " class=\"pages-select\"";
                pages += ">" + i + "</a>";
            }
            if (pageindex > 1)
                pages = first + prve + pages;
            if (pageindex < pageCount)
                pages = pages + next + last;
            return pages;
        }

        public Model.Data GetSingle(int movieId)
        {
            return dal.GetSingle(movieId);
        }

        public IList<Model.Data> GetHttpData()
        {
            return dal.GetHttpData();
        }

        public string Http2Qvod(string sHttpUrl)
        {
            string sQvodUrl = string.Empty;
            string sPostData = "url=" + sHttpUrl;
            byte[] data = Encoding.Default.GetBytes(sPostData);

            try
            {
                HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(sActionUrl);
                myrequest.Method = "POST";
                myrequest.ContentType = "application/x-www-form-urlencoded";
                myrequest.ContentLength = data.Length;
                Stream newStream = myrequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();

                HttpWebResponse myresponse = (HttpWebResponse)myrequest.GetResponse();
                StreamReader reader = new StreamReader(myresponse.GetResponseStream(), Encoding.Default);
                sQvodUrl = reader.ReadToEnd();
                myresponse.Close();
                if (sQvodUrl.IndexOf("qvod:") != -1)
                {
                    int i = sQvodUrl.IndexOf("qvod:");
                    int j = sQvodUrl.IndexOf("</textarea>");
                    sQvodUrl = sQvodUrl.Substring(i, j - i);
                    
                }
            }
            catch
            {
                sQvodUrl = sHttpUrl;
            }
            return sQvodUrl;
        }

        public bool Modify_PlayData(Model.Data d)
        {
            return dal.Modify_PlayData(d);
        }
    }
}
